package work.jame.blog.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import work.jame.blog.framework.VerificationGroup;
import work.jame.blog.framework.exception.BusinessException;
import work.jame.blog.framework.limit.RequestLimit;
import work.jame.blog.framework.result.PageResult;
import work.jame.blog.framework.result.Result;
import work.jame.blog.pojo.dto.ArticleDTO;
import work.jame.blog.pojo.vo.ArticleDetailVO;
import work.jame.blog.pojo.vo.ArticleSynopsisVO;
import work.jame.blog.pojo.vo.ArticleVO;
import work.jame.blog.service.ArticleService;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Jame
 * @since 2023-05-17
 */
@Api(tags = "文章")
@RestController
@RequestMapping("/article")
public class ArticleController {


    @Autowired
    private ArticleService articleService;

    @RequestLimit
    @ApiOperation("发布文章")
    @PostMapping("/publish")
    public Result<?> publish(@Validated(VerificationGroup.Save.class) @RequestBody ArticleDTO articleDTO) {
        Long articleId = articleService.publish(articleDTO);
        return Result.succeed(articleId);
    }


    @RequestLimit
    @ApiOperation("获取文章信息")
    @GetMapping("/getArticleInfo")
    public Result<?> getArticleInfo(@RequestParam("articleId") Long articleId) {
        ArticleDetailVO vo = articleService.getArticleDetail(articleId);
        return Result.succeed(vo);
    }


    @RequestLimit
    @ApiOperation("获取文章信息")
    @GetMapping("/queryHomeArticleSynopsis")
    public PageResult<?> queryHomeArticleSynopsis(@RequestParam("pageNumber") Long pageNumber,
                                                  @RequestParam("pageSize") Long pageSize,
                                                  @RequestParam(value = "orderRule",
                                                          required = false,
                                                          defaultValue = "1") Integer orderRule) {
        if(pageSize>10){
            throw new BusinessException("非法请求参数");
        }
        return articleService.queryHomeArticleSynopsis(pageNumber, pageSize, orderRule);
    }

}
